package com.linkit.main.article;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.linkit.config.Setting;
import com.linkit.controller.ConfigsHandler;
import com.linkit.main.AbstractPage;
import com.linkit.main.DisableOnTouchImpl;
import com.linkit.main.MainActivity;
import com.linkit.main.R;
import com.linkit.main.widget.ExtendedTextView;
import com.linkit.utils.ScreenUtils;

public class StartLayout extends AbstractPage implements OnClickListener {
	private MainActivity main;
	private RelativeLayout startAlert;
	private ProgressBar progressBar; 
	private ExtendedTextView btnOkAlert;
	private ExtendedTextView alertDescription;

	public StartLayout() {
		main = MainActivity.getInstance();
	}

	@Override
	public void setControllerId() {
	}

	@Override
	protected void setLayout() {
		// layout = new RelativeLayout(main);
		layout = (RelativeLayout) RelativeLayout.inflate(main,
				R.layout.start, null);
	}

	@Override
	public void showHeader() {
		main.contentManager.getHeaderLayout().showBackHeader(false);
	}

	@Override
	public void showContent() {

		//thiết lập trạng thái hiện thị waiting
		main.dialogManager.setVisible(false);
		
		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		
		RelativeLayout dialogContent = main.dialogManager.getDialogContent();
		RelativeLayout dialogLayout = main.dialogManager.getDialogLayout();
		
		dialogContent.removeAllViews();
		dialogContent.addView(layout, layoutParams);
		dialogLayout.setVisibility(View.VISIBLE);
		// Không bắt sự kiện ontouch bất cứ thành phần nào khi dialog hiển thị
		dialogLayout.setOnTouchListener(new DisableOnTouchImpl());
		
		btnOkAlert = (ExtendedTextView) layout
				.findViewById(R.id.alert_dialog_retry_btn);
		btnOkAlert.setTextSize(ScreenUtils.getDialogItemFontSize());
		btnOkAlert.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				ConfigsHandler configsHandler = new ConfigsHandler();
				configsHandler.doRequest(Setting.getSiteId(),
						Setting.getCatsId(), Setting.getTypes(), null);
			}
		});
		startAlert = (RelativeLayout) layout.findViewById(R.id.start_alert);
		progressBar = (ProgressBar) layout.findViewById(R.id.progressBar);
		alertDescription = (ExtendedTextView) layout
				.findViewById(R.id.alert_description);
		alertDescription.setTextSize(ScreenUtils.getDialogItemFontSize());
	}
	//Hiện thị thông báo
	public void showAlert(String desc){
		showPages();
		
		startAlert.setVisibility(View.VISIBLE);
		btnOkAlert.setVisibility(View.GONE);
		progressBar.setVisibility(View.GONE);
		
		
		// Thiết lập thông báo
		alertDescription.setText(desc);
	}
	
	//Hiện thị thông báo
	public void showProgress(){
		showPages();
		startAlert.setVisibility(View.GONE);
		progressBar.setVisibility(View.VISIBLE);
	}
	
	/**
	 * ẩn giao diện hiện thị ban đầu
	 */
	public void hidden(){
		main.dialogManager.setVisible(true);
		main.dialogManager.hideDialog();
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.alert_dialog_retry_btn:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		main.exit();
	}
}
